****************************************
*Table 2: Robots and college enrollment*
****************************************

*Main outcomes from Census
use "$final_data_outcomes/czone_Students.dta", clear
merge 1:1 czone year using "$final_data_outcomes/IPEDS_EnrollmentCZ.dta", assert(1 3) nogenerate

*Automation
merge 1:1 czone year using "$final_data_automation/czones_ExposureAutomation.dta", assert(1 3) nogenerate

*Imports
merge 1:1 czone year using "$final_data_imports/czone_ExposureChina", assert(1 3) nogenerate

*Covariates
merge m:1 czone using "$final_data_covariates/czone1990_Covariates.dta", assert(3) nogenerate
merge m:1 czone using "$final_data_covariates/IPEDS_institutionControlsCZ.dta", keep(1 3) nogenerate

xtset czone year

*Keep periods of interest
keep if year<=2000

foreach var in public communityCollege uniTop20 uniTop30Dummy uniTop30 largest150 forProfit nonProfit numberOfUni private {
replace `var' = 0 if mi(`var')
}

egen medianAnyAid = median(anyAid), by(statefip)
	replace anyAid = medianAnyAid if mi(anyAid)
	
*Panel A: OLS results
eststo clear
eststo: reghdfe d_sc expof_us_adj [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)
eststo: reghdfe d_sc expof_us_adj d_exp_us_stacked d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)
eststo: reghdfe dEnr_total11 expof_us_adj d_exp_us_stacked d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)
eststo: reghdfe dEnr_total3 expof_us_adj d_exp_us_stacked d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)
eststo: reghdfe dEnr_total12 expof_us_adj d_exp_us_stacked d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

estout using "$tables_main/Table 2a.tex", style(tex) ///
varlabels(expof_us_adj "US robot exposure") ///
cells(b(fmt(%9.3f) star) se(par)) nolabel replace mlabels(none) collabels(none) starlevels(\$^{*}\$ .1 \$^{**}\$ .05 \$^{***}\$ .01) /// 
keep(expof_us_adj) ///
order(expof_us_adj)

*Panel B: IV results
eststo clear
eststo: ivreghdfe d_sc (expof_us_adj = expof_euro7_qo) [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)
eststo: ivreghdfe d_sc (expof_us_adj d_exp_us_stacked = expof_euro7_qo d_exp_iv_stacked) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)
eststo: ivreghdfe dEnr_total11 (expof_us_adj d_exp_us_stacked = expof_euro7_qo d_exp_iv_stacked) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)
eststo: ivreghdfe dEnr_total3 (expof_us_adj d_exp_us_stacked = expof_euro7_qo d_exp_iv_stacked) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)
eststo: ivreghdfe dEnr_total12 (expof_us_adj d_exp_us_stacked = expof_euro7_qo d_exp_iv_stacked) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

estout using "$tables_main/Table 2b.tex", style(tex) ///
varlabels(expof_us_adj "US robot exposure") ///
cells(b(fmt(%9.3f) star) se(par)) nolabel replace mlabels(none) collabels(none) starlevels(\$^{*}\$ .1 \$^{**}\$ .05 \$^{***}\$ .01) /// 
keep(expof_us_adj) ///
order(expof_us_adj)

*Panel C: First-stage results:
eststo clear
eststo: reghdfe expof_us_adj expof_euro7_qo [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)
eststo: reghdfe expof_us_adj expof_euro7_qo d_exp_iv_stacked d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)
eststo: reghdfe expof_us_adj expof_euro7_qo d_exp_iv_stacked d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 if !mi(dEnr_total11) [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)
eststo: reghdfe expof_us_adj expof_euro7_qo d_exp_iv_stacked d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 if !mi(dEnr_total11) [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)
eststo: reghdfe expof_us_adj expof_euro7_qo d_exp_iv_stacked d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 if !mi(dEnr_total11) [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

estout using "$tables_main/Table 2c.tex", style(tex)  ///
varlabels(expof_euro7_qo "EU7 robot exposure$") ///
cells(b(fmt(%9.3f) star) se(par)) nolabel replace mlabels(none) collabels(none) starlevels(\$^{*}\$ .1 \$^{**}\$ .05 \$^{***}\$ .01) /// 
keep(expof_euro7_qo) ///
order(expof_euro7_qo)


